/**
 * Represents a trader
 * @author Team Love is Like Sand
 * @version 1.0
 */

import Goods.*;
import java.io.Serializable;

/**
 * Trader
 * @author kushagramansingh
 * @version 1.0
 */
public class Trader implements Serializable{
	
	/**
	 * The trader's inventory
	 */
	private Inventory inventory;
	
	/**
	 * The trader's skill level
	 */
	private int skill;
	
	/**
	 * The system to which the trader belongs
	 */
	private SolarSystem solarSystem;
	
	/**
	 * The intermediate list of prices
	 */
	private Pair[] priceList;
	
	/**
	 * The constant for the economy
	 */
	private static final int ECONOMICCONSTANT = 10000;
	
	/**
	 * Constructor for Trader
	 * @param skill
	 * @param solarSystem
	 */
	public Trader(int skill, SolarSystem solarSystem){
		this.skill = skill;
		this.solarSystem = solarSystem;
		int govt = solarSystem.getGovt();
		int tech = solarSystem.getTech();
		int resources = solarSystem.getResources();
		inventory = new Inventory((int) ((Inventory.GOODLIST.length) *
				(ECONOMICCONSTANT * (1.0 + govt / 4.0 + 7.0 / tech))));
		initializeItemList();
	}
	
	/**
	 * Returns the inventory of the Trader
	 * @return inventory
	 */
	public Inventory getInventory(){
		return inventory;
	}
	
	/**
	 * Returns the price list in an array of Pairs
	 * @return priceList
	 */
	public Pair[] getPriceList(){
		return priceList;
	}
	
	/**
	 * Returns skill level of Trader
	 * @return skill
	 */
	public int getTrade(){
		return skill;
	}
	
	/**
	 * Initialzies the list of items
	 */
	private void initializeItemList(){
		int price;
		int govt = solarSystem.getGovt();
		int tech = solarSystem.getTech();
		int resources = solarSystem.getResources();
		Good[] goods = Inventory.GOODLIST;
		priceList = new Pair[goods.length];
		for(int i = 0; i < goods.length; i++){
			price =(int) ((double) (goods[i].getBasePrice()) * (1.0 + 
					(govt * (goods[i].isLegal()?1:2)) / 4.0 + (1.0 - tech / 7.0)));
			switch(resources){
				case 1 : if(goods[i].getClass().isInstance(Candy.class)){
					price /= 2;
				}
				break;
					
				case 2 : if(goods[i].getClass().isInstance(Candy.class)){
					price *= 2;
				}
				break;
					
				case 3 : if(goods[i].getClass().isInstance(DeathMachine.class)){
					price /= 2;
				}
				break;
					
				case 4 : if(goods[i].getClass().isInstance(DeathMachine.class)){
					price *= 2;
				}
				break;
					
				case 5 : if(goods[i].getClass().isInstance(Heroin.class)){
					price /= 2;
				}
				break;
				
				case 6 : if(goods[i].getClass().isInstance(Heroin.class)){
					price *= 2;
				}
				break;
				
				case 7 : if(goods[i].getClass().isInstance(MorphAderAdrilLenol.class)){
					price /= 2;
				}
				break;
					
				case 8 : if(goods[i].getClass().isInstance(MorphAderAdrilLenol.class)){
					price *= 2;
				}
				break;
				
				case 9 : if(goods[i].getClass().isInstance(Plutonium.class)){
					price /= 2;
				}
				break;
				
				case 10 : if(goods[i].getClass().isInstance(Plutonium.class)){
					price *= 2;
				}
				break;
				
				case 11 : if(goods[i].getClass().isInstance(VideoGames.class)){
					price /= 2;
				}
				break;
				
				case 12 : if(goods[i].getClass().isInstance(VideoGames.class)){
					price *= 2;
				}
				break;
				
				case 13 : if(goods[i].getClass().isInstance(Water.class)){
					price /= 2;
				}
				break;
				
				case 14 : if(goods[i].getClass().isInstance(Water.class)){
					price *= 2;
				}
				break;
				
				default: 
					break;
			}
			priceList[i] = new Pair(goods[i], price);
			inventory.addItem(goods[i], (int) (ECONOMICCONSTANT / ((double) price)));
		}
	}
	
	/**
	 * Returns the SolarSystem the Trader is in
	 * @return solarSystem
	 */
	public SolarSystem getSystem(){
		return solarSystem;
	}
}
